package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.service.manage.tec.teaching.homework.WorkAssignService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 老师---作业预览
 * 
 */
@Api(tags = { "V01", "作业系统" })
@CrossOrigin
@RestController
public class WorkAssignQuesPreviewController extends YxyApiBaseController {

    private final static Logger LOGGER = LoggerFactory.getLogger(WorkAssignQuesPreviewController.class);

    @Autowired
    private WorkAssignService workAssignService;

    @Autowired
    private ElasticQuesService elasticQuesService;

    @Autowired
    private YxyQuesKnowRepository quesKnowRepository;

    @ApiOperation(value = "作业系统老师发布作业预览")
    @RequestMapping(value = "/api_yw/manage/tec/homework/workassign/ques/preview", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req)
            throws JsonParseException, JsonMappingException, IOException {

        Long start = System.currentTimeMillis();
        List<QuesReqModel> quesModeList = req.quesIdList;
        List<String> list = new ArrayList<String>();
        // 题的map
        Map<String, List<Integer>> quesMap = new HashMap<String, List<Integer>>();
        quesModeList.forEach(ques -> {
            list.add(ques.quesId);
            quesMap.put(ques.quesId, ques.know);
        });
        List<ElasticYxyYxyQuesResource> quesList = elasticQuesService.findByQuesIdIn(list);
        List<QuesModel> singleQuesList = new ArrayList<QuesModel>();
        List<QuesModel> multiQuesList = new ArrayList<QuesModel>();
        List<QuesModel> fillQuesList = new ArrayList<QuesModel>();
        List<QuesModel> answerQuesList = new ArrayList<QuesModel>();
        QuesModel quesModel = null;
        for (ElasticYxyYxyQuesResource eQues : quesList) {
            String questionType = eQues.getQuestionType();
            if (StringUtils.equals(questionType, "1")) {
                // 单选
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                quesModel.questionType = eQues.getQuestionType();
                // 知识点ids
                List<Integer> knowIds = quesMap.get(eQues.getId());
                quesModel.knowIds = knowIds;
                // 知识点名称
                List<YxyQuesKnow> quesKnows = quesKnowRepository.findByIdIn(knowIds);
                List<String> knowTextsList = new ArrayList<String>();
                quesKnows.forEach(quesKnow -> {
                    knowTextsList.add(quesKnow.getName());
                });
                quesModel.knowledgeTexts = knowTextsList;
                singleQuesList.add(quesModel);
            } else if (StringUtils.equals(questionType, "8")) {
                // 多选
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                quesModel.questionType = eQues.getQuestionType();
                // 知识点ids
                List<Integer> knowIds = quesMap.get(eQues.getId());
                quesModel.knowIds = knowIds;
                // 知识点名称
                List<YxyQuesKnow> quesKnows = quesKnowRepository.findByIdIn(knowIds);
                List<String> knowTextsList = new ArrayList<String>();
                quesKnows.forEach(quesKnow -> {
                    knowTextsList.add(quesKnow.getName());
                });
                quesModel.knowledgeTexts = knowTextsList;
                multiQuesList.add(quesModel);
            } else if (StringUtils.equals(questionType, "2")) {
                // 填空
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.questionType = eQues.getQuestionType();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                // 知识点ids
                List<Integer> knowIds = quesMap.get(eQues.getId());
                quesModel.knowIds = knowIds;
                // 知识点名称
                List<YxyQuesKnow> quesKnows = quesKnowRepository.findByIdIn(knowIds);
                List<String> knowTextsList = new ArrayList<String>();
                quesKnows.forEach(quesKnow -> {
                    knowTextsList.add(quesKnow.getName());
                });
                quesModel.knowledgeTexts = knowTextsList;
                fillQuesList.add(quesModel);
            } else {
                // 解答
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.questionType = eQues.getQuestionType();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                // 知识点ids
                List<Integer> knowIds = quesMap.get(eQues.getId());
                quesModel.knowIds = knowIds;
                // 知识点名称
                List<YxyQuesKnow> quesKnows = quesKnowRepository.findByIdIn(knowIds);
                List<String> knowTextsList = new ArrayList<String>();
                quesKnows.forEach(quesKnow -> {
                    knowTextsList.add(quesKnow.getName());
                });
                quesModel.knowledgeTexts = knowTextsList;
                answerQuesList.add(quesModel);
            }
        }
        // 获取知识点统计
        List<KnowModel> knowModelList = workAssignService.findKnowModelList2(quesModeList);
        // 难度统计
        Map<String, Integer> difficultMap = workAssignService.findDifficultStatic(quesList);
        // 处理响应
        RespModel respModel = new RespModel();
        respModel.quesCount = quesList.size();
        respModel.easyCount = difficultMap.get("easy");
        respModel.middleCount = difficultMap.get("middle");
        respModel.difficultCount = difficultMap.get("difficult");
        respModel.singleQuesList = singleQuesList;
        respModel.multiQuesList = multiQuesList;
        respModel.fillQuesList = fillQuesList;
        respModel.answerQuesList = answerQuesList;
        respModel.knowModelList = knowModelList;
        LOGGER.info("预览作业共用时:" + ((System.currentTimeMillis() - start) / 1000) + "s");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 试题ids
        // public List<String> quesIdList;
        // 试题请求信息
        public List<QuesReqModel> quesIdList;
    }

    public static class QuesReqModel {
        // 题的id
        public String quesId;
        // 题涉及的知识点ids
        public List<Integer> know;
    }

    public static class RespModel extends YxyApiRespModel {

        // 试题总数
        public Integer quesCount;

        // 容易题数量
        public Integer easyCount;

        // 中等题数量
        public Integer middleCount;

        // 困难题数量
        public Integer difficultCount;

        // 单选
        public List<QuesModel> singleQuesList;

        // 多选
        public List<QuesModel> multiQuesList;

        // 填空
        public List<QuesModel> fillQuesList;

        // 解答
        public List<QuesModel> answerQuesList;

        // 知识点统计
        public List<KnowModel> knowModelList;

    }

    public static class QuesModel {

        // 题id
        public String quesId;

        // 题干
        public Object quesBody;

        // 题型难度
        public String difficultyLevel;

        // 题型
        public String questionType;

        // 试题涉及的知识点
        public List<String> knowledgeTexts;

        // 试题涉及的知识点ids
        public List<Integer> knowIds;
    }

    public static class KnowModel {

        // 知识点id
        public Integer knowId;

        // 知识点名称
        public String knowName;

        // 该知识点下的试题数量
        public Integer quesCount;

        // 百分比
        public Integer percent;

    }

}
